//
//  XYCollectionViewCell.m
//  ZhaoQiPei
//
//  Created by baiye on 2017/2/5.
//  Copyright © 2017年 XiangYun. All rights reserved.
//

#import "XYBadgeView.h"

@interface XYBadgeView ()

@property (nonatomic, weak) NSLayoutConstraint * backgroundConstraint;
@property (nonatomic, weak) NSLayoutConstraint * leadingConstraint;
@property (nonatomic, weak) NSLayoutConstraint * trailingConstraint;

@end

@implementation XYBadgeView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    UIView * view = [[UIView alloc] init];
    view.translatesAutoresizingMaskIntoConstraints = NO; // 关闭系统自带的布局，不加这个，自动布局会报错
    self.backgroundView = view;
    [self addSubview:view];
    NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
    self.backgroundConstraint = constraint;
    [view addConstraint:constraint];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    
    UILabel * label = [[UILabel alloc] init];
    label.translatesAutoresizingMaskIntoConstraints = NO; // 关闭系统自带的布局，不加这个，自动布局会报错
    self.badgeLabel = label;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:10];
    [self addSubview:label];
    NSLayoutConstraint * leadingConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeLeading multiplier:1 constant:5];
    NSLayoutConstraint * trailingConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTrailing multiplier:1 constant:-5];
    [self addConstraints:@[
                           [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:label.superview attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
                           [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:label.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
                           leadingConstraint,
                           trailingConstraint
                           ]];
    self.leadingConstraint = leadingConstraint;
    self.trailingConstraint = trailingConstraint;
}

- (void)setBackgroundHeight:(CGFloat)height {
    self.backgroundConstraint.constant = height;
}

- (void)setSpace:(CGFloat)space {
    self.leadingConstraint.constant = space;
    self.trailingConstraint.constant = -space;
}

@end
